var t = require("../../api.js"), e = require("../../utils.js"), a = getApp(), i = "", n = null, o = require("../../wxParse/wxParse.js"), s = null, r = null, d = !1;

Page({
    data: {
        hide: "hide",
        time_list: {
            day: 0,
            hour: "00",
            minute: "00",
            second: "00"
        },
        p: 1,
        user_index: 0,
        show_content: !1
    },
    onLoad: function(t) {
        if (a.pageOnLoad(this, t), "undefined" == typeof my) {
            var i = decodeURIComponent(t.scene);
            if (void 0 !== i) {
                var n = e.scene_decode(i);
                n.gid && (t.goods_id = n.gid);
            }
        } else if (null !== a.query) {
            var o = a.query;
            a.query = null, t.goods_id = o.gid;
        }
        this.getGoods(t.goods_id);
    },
    getGoods: function(e) {
        var i = this;
        wx.showLoading({
            title: "加载中"
        }), a.request({
            url: t.bargain.goods,
            data: {
                goods_id: e,
                page: 1
            },
            success: function(t) {
                if (0 == t.code) {
                    var e = t.data.goods.detail;
                    o.wxParse("detail", "html", e, i), i.setData(t.data), i.setData({
                        reset_time: i.data.goods.reset_time,
                        time_list: i.setTimeList(t.data.goods.reset_time),
                        p: 1
                    }), i.setTimeOver(), t.data.bargain_info && i.getUserTime();
                } else wx.showModal({
                    title: "提示",
                    content: t.msg,
                    showCancel: !1,
                    success: function(t) {
                        t.confirm && wx.navigateBack({
                            delta: -1
                        });
                    }
                });
            },
            complete: function(t) {
                wx.hideLoading();
            }
        });
    },
    onReady: function() {
        a.pageOnReady(this);
    },
    onShow: function() {
        a.pageOnShow(this);
    },
    onHide: function() {
        a.pageOnHide(this);
    },
    onUnload: function() {
        a.pageOnUnload(this), clearInterval(n), n = null, clearInterval(s), s = null, clearInterval(r), 
        r = null;
    },
    play: function(t) {
        var e = t.target.dataset.url;
        this.setData({
            url: e,
            hide: "",
            show: !0
        }), (i = wx.createVideoContext("video")).play();
    },
    close: function(t) {
        if ("video" == t.target.id) return !0;
        this.setData({
            hide: "hide",
            show: !1
        }), i.pause();
    },
    onGoodsImageClick: function(t) {
        var e = this, a = [], i = t.currentTarget.dataset.index;
        for (var n in e.data.goods.pic_list) a.push(e.data.goods.pic_list[n].pic_url);
        wx.previewImage({
            urls: a,
            current: a[i]
        });
    },
    hide: function(t) {
        0 == t.detail.current ? this.setData({
            img_hide: ""
        }) : this.setData({
            img_hide: "hide"
        });
    },
    setTimeOver: function() {
        var t = this;
        n = setInterval(function() {
            t.data.resset_time <= 0 && clearInterval(n);
            var e = t.data.reset_time - 1, a = t.setTimeList(e);
            t.setData({
                reset_time: e,
                time_list: a
            });
        }, 1e3);
    },
    orderSubmit: function() {
        var e = this;
        var user_info = wx.getStorageSync("user_info");
        if (!user_info || !user_info.id) {
          this.setData({
            user_info_show: !0
          });
          return false;
        }
        wx.showLoading({
            title: "加载中"
        }), a.request({
            url: t.bargain.bargain_submit,
            method: "POST",
            data: {
                goods_id: e.data.goods.id
            },
            success: function(t) {
                0 == t.code ? wx.redirectTo({
                    url: "/bargain/activity/activity?order_id=" + t.data.order_id
                }) : e.showToast({
                    title: t.msg
                });
            },
            complete: function(t) {
                wx.hideLoading();
            }
        });
    },
    buyNow: function() {
        var t = [], e = [], a = this.data.bargain_info;
        a && (e.push({
            bargain_order_id: a.order_id
        }), t.push({
            mch_id: 0,
            goods_list: e
        }), wx.redirectTo({
            url: "/pages/new-order-submit/new-order-submit?mch_list=" + JSON.stringify(t)
        }));
    },
    getUserTime: function() {
        var t = this;
        s = setInterval(function() {
            t.loadData();
        }, 1e3), r = setInterval(function() {
            var e = t.data.user_index;
            t.data.bargain_info.bargain_info.length - e > 3 ? e += 3 : e = 0, t.setData({
                user_index: e
            });
        }, 3e3);
    },
    loadData: function() {
        var e = this, i = e.data.p;
        d || (d = !0, a.request({
            url: t.bargain.goods_user,
            data: {
                page: i + 1,
                goods_id: e.data.goods.id
            },
            success: function(t) {
                if (0 == t.code) {
                    var a = e.data.bargain_info.bargain_info, n = t.data.bargain_info;
                    0 == n.bargain_info.length && (clearInterval(s), s = null), n.bargain_info = a.concat(n.bargain_info), 
                    e.setData({
                        bargain_info: n,
                        p: i + 1
                    });
                } else e.showToast({
                    title: t.msg
                });
            },
            complete: function() {
                d = !1;
            }
        }));
    },
    contentClose: function() {
        this.setData({
            show_content: !1
        });
    },
    contentOpen: function() {
        this.setData({
            show_content: !0
        });
    },
    onShareAppMessage: function() {
        var t = this;
        return {
            path: "/bargain/list/list?goods_id=" + t.data.goods.id + "&user_id=" + t.data.__user_info.id,
            success: function(t) {}
        };
    }
});